<?php

namespace app\admin\controller\system;

use app\admin\controller\AuthController;
use app\model\system\SystemMenus;
use app\model\system\SystemRole;
use albert\services\JsonService;
use albert\services\UtilService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 角色管理控制器
 * Class RoleController
 * @package app\admin\controller\system
 */
class RoleController extends AuthController
{
    /**
     * 列表展示页
     * Author: Albert3306
     * Date: 2021/5/1
     * @return string
     */
    public function index(): string
    {
        return $this->fetch('system/role/index');
    }

    /**
     * 列表数据
     * Author: Albert3306
     * Date: 2021/5/1
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function lst(): string
    {
        $params = UtilService::param([
            ['page', 0],
            ['limit', 20],
            ['status', ''],
            ['keywords', ''],
        ]);
        $data = SystemRole::getLayUIPageList($params);
        return JsonService::successLayUI($data['count'], $data['data']);
    }

    /**
     * 添加展示页
     * Author: Albert3306
     * Date: 2021/5/1
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function create(): string
    {
        $menus = $this->admin['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->admin['roles']);
        return $this->fetch('system/role/create', [
            'menus' => json($menus)->getContent()
        ]);
    }

    /**
     * 添加保存
     * Author: Albert3306
     * Date: 2021/5/1
     * @return string
     */
    public function save(): string
    {
        $data = UtilService::param([
            ['name', ''],
            ['status', 0],
            ['checked_menus', [], '', 'rules'],
        ]);
        if (!$data['name']) return JsonService::fail('请输入角色名称');
        if (!is_array($data['rules']) || !count($data['rules']))
            return JsonService::fail('请选择最少一个权限');
        foreach ($data['rules'] as $v) {
            $pid = SystemMenus::where('id', $v)->value('pid');
            if (!in_array($pid, $data['rules'])) $data['rules'][] = $pid;
        }
        $data['rules'] = implode(',', $data['rules']);
        $data['level'] = $this->admin['level'] + 1;
        if (!SystemRole::create($data)) return JsonService::fail('添加失败！');
        return JsonService::successful('添加成功！');
    }

    /**
     * 编辑展示页
     * Author: Albert3306
     * Date: 2021/5/1
     * @param $id
     * @return string
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function edit($id): string
    {
        $role = SystemRole::get($id);
        $menus = $this->admin['level'] == 0 ? SystemMenus::ruleList() : SystemMenus::rolesByRuleList($this->admin['roles']);
        return $this->fetch('system/role/edit', [
            'role' => $role->toJson(),
            'menus'=> json($menus)->getContent(),
            'updateUrl' => '/admin/system/role/update/' . $id,
        ]);
    }

    /**
     * 编辑保存
     * Author: Albert3306
     * Date: 2021/5/1
     * @param $id
     * @return string
     */
    public function update($id): string
    {
        $data = UtilService::param([
            ['name', ''],
            ['status', 0],
            ['checked_menus', [], '', 'rules']
        ]);
        if (!is_array($data['rules']) || !count($data['rules']))
            return JsonService::fail('请选择最少一个权限');
        foreach ($data['rules'] as $v) {
            $pid = SystemMenus::where('id', $v)->value('pid');
            if (!in_array($pid, $data['rules'])) $data['rules'][] = $pid;
        }
        $data['rules'] = implode(',', $data['rules']);
        if (!SystemRole::edit($data, $id)) return JsonService::fail('修改失败！');
        return JsonService::successful('修改成功！');
    }

    /**
     * 删除数据
     * Author: Albert3306
     * Date: 2021/5/1
     * @param $id
     * @return string
     */
    public function delete($id): string
    {
        if (!SystemRole::del($id))
            return JsonService::fail(SystemRole::getErrorInfo('删除失败，请稍候再试！'));
        else
            return JsonService::successful('删除成功！');
    }
}